#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>

void sleep_ms(unsigned int ms)
{
	usleep(ms*1000);
}

void fcom1(void)
{
		fcom3();
		sleep_ms(27);
}

void fcom2(void)
{
		sleep_ms(35);
		fcom1();
}

void fcom3(void)
{
		sleep_ms(41);
}


void f12(void)
{
  sleep_ms(7);
  fcom1();
  fcom3();
}

void f22(void)
{
  sleep_ms(10);
  fcom2();
  fcom1();
}   

void f32(void)
{
  sleep_ms(21);
  fcom2();
}      

void f42(void)
{
  sleep_ms(11);
  fcom3();
} 

void f11(void)
{
  sleep_ms(4);
  f12();
}

void f21(void)
{
  sleep_ms(3);
  f22();
}   

void f31(void)
{
  sleep_ms(6);
  f32();
}      

void f41(void)
{
  sleep_ms(8);
  f42();
}   



static void * thread1( void* arg )
{
		printf("%s pid %d\n", __FUNCTION__, syscall(SYS_gettid));
		while(1)
		{
				f11();
				sleep_ms(10);
		}
}

static void * thread2( void* arg )
{
		printf("%s pid %d\n", __FUNCTION__, syscall(SYS_gettid));
		while(1)
		{
				f21();
				sleep_ms(10);
		}
}

static void * thread3( void* arg )
{
		printf("%s pid %d\n", __FUNCTION__, syscall(SYS_gettid));
		while(1)
		{
				f31();
				sleep_ms(10);
		}
}

static void * thread4( void* arg )
{
		printf("%s pid %d\n", __FUNCTION__, syscall(SYS_gettid));
		while(1)
		{
				f41();
				sleep_ms(10);
		}
}

int main(void)
{
	pthread_attr_t      stThreadAttr;
	pthread_attr_init( &stThreadAttr );
	pthread_t t1,t2,t3,t4;
	int flag;
	
	//__cyp_profile_init_list();
	
	pthread_create( &t1, &stThreadAttr, thread1, NULL );
	pthread_create( &t2, &stThreadAttr, thread2, NULL );
	pthread_create( &t3, &stThreadAttr, thread3, NULL );
	pthread_create( &t4, &stThreadAttr, thread4, NULL );
		/*
		while(1)
		{
				__cyp_profile_check_list(1);
				sleep_ms(1000);
		}
   */
	sleep_ms(5000);
	printf("Kill thread\n");
	sleep_ms(1000);
	pthread_kill(t1, SIGCONT);
	pthread_kill(t2, SIGCONT);
	pthread_kill(t3, SIGCONT);
	pthread_kill(t4, SIGCONT);

	printf("Kill end\n");
	return 0;
 }
